<?php
function deletename($value1, $value2)
{
    //建立数据库连接
    require '../script/mysqlconfig.php';
    $conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
    if ($conn->connect_error) {
        die("连接数据库失败: " . $conn->connect_error);
    }
    $value = trim($_POST["$value1"]);
    if (empty($value)) {
        echo "输入的".$value2."字段是空白，删除失败！" . "<br/>";
    } else {
        $sql = "DELETE FROM $value1 WHERE name = '$value'";
        if ($conn->query($sql) === true) {
            echo "字段删除成功。";
        } else {
            echo "字段删除失败！请检查输入的信息。" . "<br/>" . $conn->error;
        }
    }
    //关闭数据库连接
    $conn->close();
}

if (isset($_POST['brand'])) {
    deletename("brand", "设备品牌");
} elseif (isset($_POST['room'])) {
    deletename("room", "机房");
} elseif (isset($_POST['cabinet'])) {
    deletename("cabinet", "机柜");
} elseif (isset($_POST['ip'])) {
    deletename("ip", "IP地址");
} else {
    echo "请检查输入的信息，要删除的字段不存在！" . "<br/>";
}


////建立数据库连接
//require '../script/mysqlconfig.php';
//$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
//if ($conn->connect_error) {
//    die("连接数据库失败: " . $conn->connect_error);
//}
//
//if (isset($_POST['brand'])) {
//    $value1 = trim($_POST["brand"]);
//    if (empty($value1)) {
//        echo "输入的设备品牌字段是空白，删除失败！" . "<br/>";
//    } else {
//        $sql = "DELETE FROM brand WHERE name = '$value1'";
//        if ($conn->query($sql) === true) {
//            echo "字段删除成功。";
//        } else {
//            echo "字段删除失败！请检查输入的信息。" . "<br/>" . $conn->error;
//        }
//    }
//} elseif (isset($_POST['room'])) {
//    $value1 = trim($_POST["room"]);
//    if (empty($value1)) {
//        echo "输入的机房字段是空白，删除失败！" . "<br/>";
//    } else {
//        $sql = "DELETE FROM room WHERE name = '$value1'";
//        if ($conn->query($sql) === true) {
//            echo "字段删除成功。";
//        } else {
//            echo "字段删除失败！请检查输入的信息。" . "<br/>" . $conn->error;
//        }
//    }
//} elseif (isset($_POST['cabinet'])) {
//    $value1 = trim($_POST["cabinet"]);
//    if (empty($value1)) {
//        echo "输入的机柜字段是空白，删除失败！" . "<br/>";
//    } else {
//        $sql = "DELETE FROM cabinet WHERE name = '$value1'";
//        if ($conn->query($sql) === true) {
//            echo "字段删除成功。";
//        } else {
//            echo "字段删除失败！请检查输入的信息。" . "<br/>" . $conn->error;
//        }
//    }
//} elseif (isset($_POST['ip'])) {
//    $value1 = trim($_POST["ip"]);
//    if (empty($value1)) {
//        echo "输入的机柜字段是空白，删除失败！" . "<br/>";
//    } else {
//        $sql = "DELETE FROM ip WHERE name = '$value1'";
//        if ($conn->query($sql) === true) {
//            echo "字段删除成功。";
//        } else {
//            echo "字段删除失败！请检查输入的信息。" . "<br/>" . $conn->error;
//        }
//    }
//} else {
//    echo "请检查输入的信息，要删除的字段不存在！" . "<br/>";
//}
//
////关闭数据库连接
//$conn->close();